You are given three integers n
, m
and k
. Consider the following algorithm to find the maximum element of an array of positive integers:
You should build the array arr which has the following properties:
arr
has exactlyn
integers.1 <= arr[i] <= m
where(0 <= i < n)
.- After applying the mentioned algorithm to
arr
, the valuesearch_cost
is equal tok
.
Return the number of ways to build the array arr
under the mentioned conditions. As the answer may grow large, the answer must be computed modulo 109 + 7
.
Input: n = 2, m = 3, k = 1 Output: 6 Explanation: The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]
Input: n = 5, m = 2, k = 3 Output: 0 Explanation: There are no possible arrays that satisify the mentioned conditions.
Input: n = 9, m = 1, k = 1 Output: 1 Explanation: The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]
1 <= n <= 50
1 <= m <= 100
0 <= k <= n
implSolution{pubfnnum_of_arrays(n:i32,m:i32,k:i32) -> i32{constMOD:i64 = 1_000_000_007;letmut dp = vec![vec![vec![0; m asusize + 1]; k asusize + 1]; n asusize + 1]; dp[0][0][0] = 1;for a in0..dp.len() - 1{for b in0..dp[0].len(){for c in0..dp[0][0].len(){ dp[a + 1][b][c] = (dp[a + 1][b][c] + dp[a][b][c]* c asi64) % MOD;if b < dp[0].len() - 1{for d in c + 1..dp[0][0].len(){ dp[a + 1][b + 1][d] = (dp[a + 1][b + 1][d] + dp[a][b][c]) % MOD;}}}}} dp[n asusize][k asusize].iter().fold(0, |acc, x| (acc + x) % MOD)asi32}}